<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\AppException;
use App\Models\Company;
use App\Models\Config;
use App\Models\Data;
use App\Models\Hospital;
use App\Models\Member;
use App\Models\MemberAnswer;
use App\Models\Problem;
use App\Models\QuestionMember;
use App\Models\Vote;
use App\Models\VoteT;
use App\Models\Wages;
use App\Models\WagesUpload;
use App\Services\ExcelExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class WagesController extends BaseController
{

    /**
     *
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        $params = $request->all();
        $build = Wages::query();
        $pageSize = $request->input('pageSize', 50);

        if (isset($params['name'])) {
            $build = $build->where('name', $params['name']);
        }

        if (isset($params['year'])) {
            $build = $build->where('year', $params['year']);
        }

        if (isset($params['month'])) {
            $build = $build->where('month', intval($params['month']));
        }

        if (isset($params['company_id'])) {
            $build = $build->where('company_id', $params['company_id']);
        }

        if (isset($params['id_card'])) {
            $build = $build->where('id_card', $params['id_card']);
        }
        if (isset($params['mobile'])) {
            $build = $build->where('mobile', $params['mobile']);
        }
        $data = $build->orderBy('w_id', 'desc')->paginate($pageSize);
        $data = json_decode(json_encode($data), true);
        $data = $this->structList($data);
        return $this->success($data);
    }

    public function updateWages(Request $request): array
    {
        $params = $request->all();
        if (!isset($params['id']) || empty($params['id'])) {
            return $this->errorCode('10001', [], 'id参数输入错误');
        }

        $id = $params['id'];
        unset($params['id']);
        unset($params['token']);
        $params['json'] = json_encode($params['json']);
        $re = Wages::query()->where('w_id', $id)->update($params);
        return $this->success($re);
    }

    public function structList($data)
    {
        foreach ($data['data'] as &$value) {
            $value['header_json'] = json_decode($value['header_json']);
            $value['json'] = json_decode($value['json']);
        }
        return $data;
    }

    public function getWagesByMobile(Request $request)
    {
        $params = $request->all();
        $pageSize = $request->input('pageSize', 50);
        if (!isset($params['name']) || empty($params['name'])) {
            return $this->errorCode('10001', [], '姓名参数输入错误');
        }

        if (!isset($params['id_card']) || empty($params['id_card'])) {
            return $this->errorCode('10001', [], '身份证号输入错误');
        }

        $data = Wages::query()->where(['id_card' => $params['id_card'], 'name' => $params['name']])->orderBy('w_id', 'desc')->paginate($pageSize);
        $data = json_decode(json_encode($data), true);
        $data = $this->structList($data);
        return $this->success($data);
    }

    public function getDetailById(Request $request): array
    {
        $params = $request->all();
        $id = $params['id'];
        $info = Wages::query()->where('w_id', $id)->first()->toArray();
        $info['header_json'] = json_decode($info['header_json']);
        $info['json'] = json_decode($info['json']);
        return $this->success($info);
    }

    public function updateDataById(Request $request)
    {
        $params = $request->all();

        if (empty($params['id'])) {
            return $this->errorCode('10001', [], 'id为空');
        }
        $id = $params['id'];

        if (!isset($params['name']) || empty($params['name'])) {
            return $this->errorCode('10001', [], '姓名参数输入错误');
        }

        if (!isset($params['mobile']) || empty($params['mobile'])) {
            return $this->errorCode('10001', [], '手机号输入错误');
        }
    }


    /**
     * 获取上传表格记录
     * @param Request $request
     * @return array
     */
    public function getUploadCord(Request $request): array
    {
        $params = $request->all();
        $build = WagesUpload::query();
        $pageSize = $request->input('pageSize', 50);

//        if (isset($params['name'])) {
//            $build = $build->where('name', $params['name']);
//        }
//
//        if (isset($params['email'])) {
//            $build = $build->where('email', $params['email']);
//        }
//        if (isset($params['mobile'])) {
//            $build = $build->where('mobile', $params['mobile']);
//        }
//
//        if (isset($params['password'])) {
//            $build = $build->where('password', $params['password']);
//        }
        $data = $build->orderBy('aid', 'desc')->paginate($pageSize);
        return $this->success($data);

    }

    public function deleteDataById(Request $request): array
    {
        $params = $request->all();
        $id = $params['id'];
        $info = Wages::query()->where('w_id', $id)->delete();
        return $this->success($info);
    }

    /**
     * 根据上传工资条的记录去删除 产生的数据
     * @return array
     */
    public function deleteAllCordByUploadId(Request $request): array
    {
        $params = $request->all();
        $id = $params['id'];
        $info = Wages::query()->where('aid', $id)->delete();
        $info = WagesUpload::query()->where('aid', $id)->delete();
        return $this->success($info);
    }


    public function getCompanyList(Request $request): array
    {
        $params = $request->all();
        $info = Company::query()->get();
        return $this->success($info);
    }

    public function addCompany(Request $request): array
    {
        $params = $request->all();
        if (!isset($params['name']) || empty($params['name'])) {
            return $this->errorCode('10001', [], 'name不能为空');

        }
        $info = Company::query()->create(['name' => $params['name']]);
        return $this->success($info);
    }

    public function updateCompany(Request $request): array
    {
        $params = $request->all();
        if (!isset($params['name']) || empty($params['name'])) {
            return $this->errorCode('10001', [], 'name不能为空');
        }
        if (!isset($params['id']) || empty($params['id'])) {
            return $this->errorCode('10001', [], 'id不能为空');
        }
        $info = Company::query()->where('c_id', $params['id'])->update(['name' => $params['name']]);
        return $this->success($info);
    }

}
